package com.audaque.springboot.foshanupload.reactor.webfluxdemo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author zgb
 * @desc 用处不大：不在同一条链，不能取放
 * @date 2023-08-03 18:06:53
 */

@Slf4j
@RestController
@RequestMapping("subscriber")
public class SubscriberController {
    //不在同一条链，不能取放
    @GetMapping("/subscriberContextPut")
    public Mono<Void> subscriberContextPut() {
        Mono.empty().subscriberContext(ctx ->{return ctx.put("token", "Reactor");});
        return Mono.empty();
    }
    //不在同一条链，不能取放
    @GetMapping("/subscriberContextGet")
    public Mono<String> subscriberContextGet() {
        return Mono.subscriberContext().map(ctx -> ctx.get("token"));
    }


    @GetMapping("subscriberContextPutGet")
    public Mono<String> subscriberContextPutGet() {
        String key = "message";
        return Mono.just("Hello")
                //订阅是从下游流上上游的 ，只对它上游的操作生效，对下游的操作不生效。所以取到
                //取
                .flatMap(s -> Mono.subscriberContext().map(ctx -> s + " " + ctx.getOrDefault(key, "")))
                //放
                .subscriberContext(ctx -> ctx.put(key, "World"));

    }

    @GetMapping("subscriberContextPutGetGet1")
    public Mono<String> subscriberContextPutGetGet1() {
        String key = "message";
        return Mono.just("Hello")
                //订阅是从下游流上上游的
                //取
                .flatMap(s -> Mono.subscriberContext().map(ctx -> s + " " + ctx.get(key)))
                //取
                .flatMap(s -> Mono.subscriberContext().map(ctx -> s + " " + ctx.get(key)))
                //放
                .subscriberContext(ctx -> ctx.put(key, "World"));

    }
    @GetMapping("subscriberContextPutGetGet2")
    public Mono<Void> subscriberContextPutGetGet2() {
        String key = "message";
        //不在同一条链，不能取放
        //放
        Mono.empty().subscriberContext(ctx -> ctx.put(key, "World"));
        AtomicReference<String> str = new AtomicReference();
        Mono.subscriberContext().map(ctx -> ctx.getOrDefault(key, "")).subscribe(e -> str.set(e));
        log.debug("str:" + str);
        AtomicReference<String> str1 = new AtomicReference();
        Mono.subscriberContext().map(ctx -> ctx.getOrDefault(key, "")).subscribe(e -> str1.set(e));
        log.debug("str1:" + str1);
        return Mono.empty();
    }
    @GetMapping("subscriberContextPutGetGet3")
    public Mono<String> subscriberContextPutGetGet3() {
        String key = "message";
        //在同一条链，能取放
        return Mono.just("Hello")
                //订阅是从下游流上上游的 ，只对它上游的操作生效，对下游的操作不生效。所以取到
                //取
                .flatMap(s -> Mono.subscriberContext().map(ctx ->ctx.getOrDefault(key, ""))
                        .map(e-> s+e)
                        //从容器取出必须用flatMap，可以取出多次
                        .flatMap(e->Mono.subscriberContext().map(ctx ->e+ctx.getOrDefault(key, "")))
                )
                //放
                .subscriberContext(ctx -> ctx.put(key, "World"));

    }



    @GetMapping("subscriberContextPutPutGet")
    public Mono<String> subscriberContextPutPutGet() {
        String key = "message";
        return Mono.just("Hello")
                //订阅是从下游流上上游的 ，只对它上游的操作生效，对下游的操作不生效。所以取到
                //取
                .flatMap(s -> Mono.subscriberContext().map(ctx -> s + " " + ctx.getOrDefault(key, "")))
                //放 :有下向上，新的覆盖旧的
                .subscriberContext(ctx -> ctx.put(key, "Reactor"))
                .subscriberContext(ctx -> ctx.put(key, "World2"));

    }

    @GetMapping("subscriberContextPutGetPutGet")
    public Mono<String> subscriberContextPutGetPutGet() {
        String key = "message";
        return Mono.just("Hello")
                //取
                .flatMap(s -> Mono.subscriberContext().map(ctx -> s + " " + ctx.get(key)))
                //放
                .subscriberContext(ctx -> ctx.put(key, "Reactor"))
                //取
                .flatMap(s -> Mono.subscriberContext().map(ctx -> s + " " + ctx.get(key)))
                //放
                .subscriberContext(ctx -> ctx.put(key, "World"));

    }


    @GetMapping("subscriberContextPut_PutGet_Get")
    public Mono<String> subscriberContextPut_PutGet_Get() {
        String key = "message";
        return Mono.just("Hello")
                //流2
                .flatMap(s -> Mono.subscriberContext().map(ctx -> s + " " + ctx.get(key)))
                //流1
                .flatMap(s -> Mono.subscriberContext().map(ctx -> s + " " + ctx.get(key)).subscriberContext(ctx -> ctx.put(key, "Reactor")))
                //流2
                .subscriberContext(ctx -> ctx.put(key, "World"));

    }


    @GetMapping("subscriberContextGetPut")
    public Mono<String> subscriberContextGetPut() {
        String key = "message";
        return Mono.just("Hello")
                //订阅是从下游流上上游的,所以取不到
                //放
                .subscriberContext(ctx -> ctx.put(key, "World"))
                //取
                .flatMap(s -> Mono.subscriberContext().map(ctx -> s + " " + ctx.getOrDefault(key, "")));


    }


}
